package com.example.asus.testdesign;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    RadioButton r1=null;
    RadioButton r2=null;
    RadioButton r3=null;
    RadioButton r4=null;
    RadioGroup radioGroup = null;
    RadioButton currentRadioButton=null;
    TextView tx1=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        r1=(RadioButton)findViewById(R.id.a);
        r2=(RadioButton)findViewById(R.id.b);
        r3=(RadioButton)findViewById(R.id.c);
        r4=(RadioButton)findViewById(R.id.d);
        tx1=(TextView)findViewById(R.id.moitor);

        //radioGroup.setOnCheckedChangeListener(mChangeRadio);
        Button btn1_sure =(Button)findViewById(R.id.sure);
        Button btn2_cancel =(Button)findViewById(R.id.cancel);
        btn1_sure.setOnClickListener(new btn1_sure());
        btn2_cancel.setOnClickListener(new btn2_cancel());
    }

    class btn1_sure implements View.OnClickListener {
        @Override//方法重写
        public void onClick(View v) {
            String ans="";
            if(r1.isChecked()){
                ans="Clark Kent";
            }
            if(r2.isChecked()){
                ans="Bruce Wayne";
            }
            if(r3.isChecked()){
                ans="Barry Allen";
            }
            if(r4.isChecked()){
                ans="Thomas Bergersen";
            }
            //new一个Intent对象，并指定class
            Intent intent=new Intent();
            //设置Intent对象要启动的Activity
            intent.setClass(MainActivity.this,OtherActivity.class);
            //new一个Bundle对象，并将要传递的数据传入
            Bundle bundle=new Bundle();
            bundle.putString("ans",ans);
            //将Bundle对象assign给Intent
            intent.putExtras(bundle);

            //通过Intent对象启动另一个activity
            startActivityForResult(intent,0);
        }
    }
    class btn2_cancel implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            radioGroup.clearCheck() ;
            setTitle("");
        }
    }
   /* private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == r1.getId()) {
                currentRadioButton=r1;
            } else if (checkedId == r2.getId()) {
                currentRadioButton=r2;
            } else if (checkedId == r3.getId()) {
                currentRadioButton=r3;
            } else if (checkedId == r4.getId()) {
                currentRadioButton=r4;
            }
        }*/
    protected  void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==0&&resultCode==RESULT_OK){
                Bundle bunde=data.getExtras();
                String ans=bunde.getString("ans");
                String text1="back from OtherActivity, return value: "+ans;
                tx1.setText(text1);
                setTitle("back from : "+ans);
        }
    }
}

